package com.zy.datareceiver.binsess.warning.service.impl;

import com.zy.datareceiver.binsess.decode.service.StorageTaskService;
import com.zy.datareceiver.binsess.warning.email.service.EmailService;
import com.zy.datareceiver.binsess.warning.entity.WarningData;
import com.zy.datareceiver.core.cast.CacheDict;
import com.zy.datareceiver.core.cast.ConCast;
import com.zy.datareceiver.core.common.SpringApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

public class SendWarningMsg extends Thread {

    private HashMap<String,Long> sendTimeMap = new HashMap<>();
    private EmailService emailService = SpringApplicationContext.getBean(EmailService.class);

    private static final Logger log = LoggerFactory.getLogger(SaveWarningDataDB.class);
    @Override
    public void run() {
        while(true){
            try {
                WarningData warningData = StorageTaskService.sendMsgQueue.take();


                String pointName = CacheDict.getPointNameByPointId(warningData.getPointId());
                String parameterName = CacheDict.getParameterNameByParameterId(warningData.getParameterId());
                if (pointName == null || parameterName == null){
                    return ;
                }
                String phoneMsg = pointName + "-" +parameterName;
                String warnLevel = CacheDict.getWarningLevelNameByLevelId(warningData.getLevelId());
                String sendMsg = phoneMsg + warnLevel;


                if (!isSended(sendMsg)){
                    //sendMsgAndSaveDB(warningData);
                }
            } catch (InterruptedException e) {
                log.error(" 预警数据入库出现异常，异常信息 {}",e);            }
        }
    }

    @Transactional
    public void sendWarningMsgAndSaveDB(WarningData warningData) {
//        List<SendInfo> sendInfoList = new ArrayList<>();
//        boolean successSms = sendSmsMsg(warningData);
//        boolean successEmail = sendEmailInfo(warningData);
//        SendInfo sendInfo = new SendInfo();
//        sendInfo.setType();
//        sendInfo.setStatus();
    }

    /**
     * type 表示是预警（0）还是解除预警（1）
     * */
    private boolean sendEmailInfo(WarningData warningData,Integer type) {
        return false;
    }

    /**
     * type 表示是预警（0）还是解除预警（1）
     * */
    private boolean sendSmsMsg(WarningData warningData,Integer type) {
        return false;
    }

    private boolean isSended(String sendMsg) {
        if (sendTimeMap.containsKey(sendMsg)){
            long now = System.currentTimeMillis();
            if (now -sendTimeMap.get(sendMsg) < ConCast.Sms.MESSAGE_DELIVERY_INTERVAL_TIME){
                sendTimeMap.put(sendMsg,System.currentTimeMillis());
                return true;
            }
        }else{
            sendTimeMap.put(sendMsg,System.currentTimeMillis());
            return true;
        }
        sendTimeMap.put(sendMsg,System.currentTimeMillis());
        return false;
    }
}
